package com.jsb.laundry.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.jsb.laundry.common.enums.ReturnStatusCode;
import com.jsb.laundry.common.response.WebResponse;
import com.jsb.laundry.common.utils.CodeUtil;
import com.jsb.laundry.common.utils.JSBUtil;
import com.jsb.laundry.common.utils.spring.SpringUtil;
import com.jsb.laundry.enums.LoginType;
import com.jsb.laundry.model.dto.LoginRequest;
import com.jsb.laundry.service.designpatterns.adapter.loginservice.LoginAdapter;
import com.jsb.laundry.utils.RedisUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.concurrent.TimeUnit;

@Api(tags = {"骑手登录"})
@RestController
@RequestMapping("/login/")
public class LoginController {
    private static final Logger LOGGER = LoggerFactory.getLogger(LoginController.class);
    @Resource
    private RedisUtil redisUtil;
    /**
     * 骑手登录
     * 手机端 小程序端
     *
     * @param loginRequest
     * @return
     */
    @ApiOperation(value = "骑手登录", notes = "骑手登录", httpMethod = "POST")
    @Transactional
    @PostMapping("lingIng")
    public synchronized WebResponse lingIng(@RequestBody LoginRequest loginRequest) {
        //获取客户端传送过来的登录类型
        String loginType = loginRequest.getLoginType();
        if (loginType == null) {
            return WebResponse.success(ReturnStatusCode.PLEASE_FILL_IN_THE_PAYMENT_METHOD.getCode(), ReturnStatusCode.PLEASE_FILL_IN_THE_PAYMENT_METHOD.getMsg());
        }
        //拿到传输过来的值去找对应的类
        Class<? extends LoginAdapter> aClass = LoginType.containsLoginType(loginType);
        if (aClass == null) {
            return WebResponse.success(ReturnStatusCode.THERE_IS_NO_SUCH_PAYMENT_METHOD.getCode(), ReturnStatusCode.THERE_IS_NO_SUCH_PAYMENT_METHOD.getMsg());
        }

        LoginAdapter adapter = SpringUtil.getBean(aClass);
        //找对应的方法
//            LoginAdapter adapter = aClass.getDeclaredConstructor().newInstance();
        //请求参数
        Object result = adapter.login(loginRequest);
        return WebResponse.successData(result);

    }
    @ApiOperation(value = "发送验证码", notes = "发送验证码", httpMethod = "POST")
    @PostMapping("/endPhoneCode")
    public WebResponse endPhoneCode(@RequestBody String data) {
        JSONObject jsonObject = JSON.parseObject(data);
        String phone = jsonObject.getString("phone");
        if (JSBUtil.isEmptyObject(phone)){
            return WebResponse.failOfParameter();
        }
        //随机4位验证码
        String phoneCode = JSBUtil.randomGenStrs(4);
        CodeUtil.EndCode(phone, phoneCode);
        redisUtil.setCacheObject(phone, phoneCode, 5, TimeUnit.MINUTES);
        return WebResponse.successData("1");
    }
}
